package com.ruoyi.customer.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.customer.domain.Customer;
import com.ruoyi.customer.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;

    /**
     * 查询顾客列表
     */
    @RequiresPermissions("customer:list")
    @GetMapping("/list")
    public TableDataInfo list(Customer customer) {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 模糊查询顾客列表
     */
    @GetMapping("/select")
    public TableDataInfo select(String customerName) {
        startPage();
        List<Customer> list = customerService.selectCustomerListBycustomerName(customerName);
        return getDataTable(list);
    }



    /**
     * 获取顾客详细信息
     */
    @GetMapping("/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId) {
        return AjaxResult.success(customerService.selectCustomerById(customerId));
    }

    /**
     * 新增顾客
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Customer customer) {
        if (customerService.insertCustomer(customer) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("新增顾客失败");
    }

    /**
     * 修改顾客
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer) {
        if (customerService.updateCustomer(customer) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改顾客失败");
    }

    /**
     * 修改顾客状态
     */
    @PutMapping("/changeStatus/{customerId}")
    public AjaxResult changeCustomerStatus(@PathVariable("customerId") Long customerId, @RequestParam("status") String status) {
        int rows = customerService.changeCustomerStatus(customerId, status);
        if (rows > 0) {
            return AjaxResult.success("修改顾客状态成功");
        }
        return AjaxResult.error("修改顾客状态失败");
    }


    /**
     * 删除顾客
     */
    @DeleteMapping("/{customerId}")
    public AjaxResult remove(@PathVariable Long customerId) {
        return toAjax(customerService.deleteCustomerById(customerId));
    }
}
